<?php
    $this->breadcrumbs = array(
    'Manage Templates' => array('manageTemplates'),
    'Edit',
    );

    //    echo hasParam('SupportSettings::CONTACT_ROOT_CATEGORY_ID');
?>
<h1>Edit email templates</h1>
<div style="width:70%;float:left;">
    <div class="form wide">
        <?php $form=$this->beginWidget('CActiveForm', array(
            'id'=>'email-form',
            'enableAjaxValidation'=>false          
            )); ?>
        <div class="row">
            <?php echo $form->labelEx($model,'name'); ?>
            <?php echo $form->textField($model,'name',array('style' => 'width:400px')); ?>       
            <?php echo $form->error($model,'name'); ?>
        </div>

        <div class="row">
            <?php echo $form->labelEx($model,'sender_name'); ?>
            <?php
                $this->widget('CAutoComplete',
                array(
                'model'=>$model,
                'attribute'=>'sender_name', 
                'url'=>array('lookupSenderEmail','type'=>'name'), 
                'max'=>10, //specifies the max number of items to display
                'minChars'=>1,                  
                'matchCase'=>false, //match case when performing a lookup?
                'htmlOptions'=>array('size'=>'40'), 

                //'methodChain'=>".result(function(event,item){\$(\"#Source_update_user_id\").val(item[1]);})",
                ));
            ?>
            <?php echo $form->error($model,'sender_name'); ?>
        </div>
        <div class="row">
            <?php echo $form->labelEx($model,'sender_email'); ?>
            <?php
                $this->widget('CAutoComplete',
                array(
                'model'=>$model,
                'attribute'=>'sender_email', 
                'url'=>array('lookupSenderEmail','type'=>'email'), 
                'max'=>10, //specifies the max number of items to display
                'minChars'=>1,                  
                'matchCase'=>false, //match case when performing a lookup?
                'htmlOptions'=>array('size'=>'40'), 

                //'methodChain'=>".result(function(event,item){\$(\"#Source_update_user_id\").val(item[1]);})",
                ));
            ?>
            <?php echo $form->error($model,'sender_name'); ?>
        </div>

        <div class="row">
            <?php echo $form->labelEx($model,'subject'); ?>
            <?php echo $form->textField($model,'subject',array('style' => 'width:400px')); ?>       
            <?php echo $form->error($model,'subject'); ?>	    
        </div>
        <div class="row">
            <?php echo $form->labelEx($model,'template'); ?>
            <?php
                if(preg_match_all('/\<\?php echo \$?(.*?);\?\>/',$content,$match)){                    
                    foreach($match[0] as $key=>$matchItem){
                        $content=str_replace($matchItem,"&lt;#".$match[1][$key]."#&gt;",$content);
                    }                   
                }                 
                $this->widget('Core.components.tinymce.ETinyMce', array(
                'name' => 'content',
                'value' => $content,
                'editorTemplate' => 'full',
                'useElFinder' => false,
                'options' => array(
                'theme_advanced_buttons1' => 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,removeformat,cleanup,|,spellchecker,|,visualaid,visualchars,|,ltr,rtl,|,code,preview,fullscreen',   
                'setup'=>'js:function (ed){
                ed.onClick.add(
                function(ed, e){
                $("#target-focused").val(2);
                })
                }',
                ),

                'width' => '500px',
                'height' => '400px',
                'useCompression' => false,
                ));
            ?>
            <?php echo CHtml::hiddenField('target-focused','',array('id'=>'target-focused')); ?>
            <?php echo $form->error($model,'template'); ?>	
        </div>

        <div class="row buttons">
            <?php echo CHtml::submitButton('Save'); ?>
        </div>
        <?php $this->endWidget(); ?>
    </div>



    <?php
        $script = '
        $("#templates").change(function(){
        location = "' . url('/Admin/tools/emailTemplates/file') . '/"+this.value;
        })
        ';
        cs()->registerScript('template-selected', $script, CClientScript::POS_READY);
    ?>
</div>
<div id="email-parameter-list" style="width:30%;float:right;">
    <?php if (isset($paramList) > 0) : ?>
        <h4> Availible Parameters</h4>
        <p class="hint" style="display: none;font-size:11px;font-style: italic;">Please focus in subject or template and click parameter that want to use</p>  
        <ul class="list-params">
            <?php
                $paramList = array_keys($paramList);
                foreach ($paramList as $key => $value) {
                    echo "<li><a>" . $value . "</a></li>";
                }
            ?>
        </ul>

        <?php
            $script = '
            jQuery.fn.extend({
            insertAtCaret: function(myValue){
            return this.each(function(i) {
            if (document.selection) {
            //For browsers like Internet Explorer
            this.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
            this.focus();
            }
            else if (this.selectionStart || this.selectionStart == "0") {
            //For browsers like Firefox and Webkit based
            var startPos = this.selectionStart;
            var endPos = this.selectionEnd;
            var scrollTop = this.scrollTop;
            this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
            this.focus();
            this.selectionStart = startPos + myValue.length;
            this.selectionEnd = startPos + myValue.length;
            this.scrollTop = scrollTop;
            } else {
            this.value += myValue;
            this.focus();
            }
            })
            }
            });   


            $("#EmailMessage_subject").focus(function(){
            $("#target-focused").val(1);
            });


            $(".list-params li a").click(function(){    
            var html="<#"+$(this).html()+"#>";
            if($("#target-focused").val()==1){
            $("#EmailMessage_subject").insertAtCaret(html);
            }
            else if($("#target-focused").val()==2){
            tinyMCE.execInstanceCommand("content","mceInsertContent",false,html);   
            }            



            return false;
            })
            ';
            cs()->registerScript('select-template', $script, CClientScript::POS_LOAD);
            endif;
    ?>
</div>
<br style="clear:both;"/>